import configparser
import os


# 获取config.ini文件中的配置数据
class Config:

    def __init__(self):
        self.config = configparser.ConfigParser()
        self.path = os.path.dirname(os.path.split(os.path.realpath(__file__))[0]) + '/testData/config.ini'

    def get_config(self, section, key=None):
        """
        :param section: section
        :param key: key传了就以字符串格式返回指定key的数据，未传就列表形式返回当前section下的所有数据
        格式如下：[('test_url', 'https://www.kanman.com'), ('account', '15928152911'), ('password', '1234abcd')]
        :return:
        """
        self.config.read(self.path, encoding="utf-8-sig")
        if key:
            config = self.config.get(section, key)
            if config == "True" or config == "true":
                config = True
            if config == "False" or config == "false":
                config = False
        else:
            config = self.config.items(section)
            length = len(config)
            for i in range(length):
                config[i] = list(config[i])  # tuple转为list,tuple不可修改需转为list进行修改
            for i in range(length):
                if config[i][1] == "True" or config[i][1] == "true":
                    config[i][1] = True
                if config[i][1] == "False" or config[i][1] == "false":
                    config[i][1] = False
        return config

con = Config()  # 实例化config
